/*
 * File: SOH_cycle.c
 *
 * Code generated for Simulink model 'SOH_cycle'.
 *
 * Model version                  : 1.19
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Thu Sep 10 15:11:54 2020
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: STMicroelectronics->ST10/Super10
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "SOH_cycle.h"
#include "SOH_cycle_private.h"

/* Imported  block signals */
real32_T AccDchaAH;             /* '<S2>/Add2' */
real32_T AccChaAH;              /* '<S1>/Add3' */
real32_T BatSoh;                /* '<S3>/SOH_Life' */
real32_T AccDchaAH_EE;          /* '<Root>/AccDchaAH_EE' */
real32_T AccChaAH_EE;           /* '<Root>/AccChaAH_EE' */

/* Block states (auto storage) */
DW_SOH_cycle_T SOH_cycle_DW;

/* External outputs (root outports fed by signals with auto storage) */
ExtY_SOH_cycle_T SOH_cycle_Y;

/* Real-time model */
RT_MODEL_SOH_cycle_T SOH_cycle_M_;
RT_MODEL_SOH_cycle_T *const SOH_cycle_M = &SOH_cycle_M_;
real32_T look1_iflf_binlcapw(real32_T u0, const real32_T bp0[], const real32_T
  table[], uint32_T maxIndex)
{
  real32_T y;
  real32_T frac;
  uint32_T iRght;
  uint32_T iLeft;
  uint32_T bpIdx;

  /* Lookup 1-D
     Search method: 'binary'
     Use previous index: 'off'
     Interpolation method: 'Linear'
     Extrapolation method: 'Clip'
     Use last breakpoint for index at or above upper limit: 'on'
     Remove protection against out-of-range input in generated code: 'off'
   */
  /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Clip'
     Use previous index: 'off'
     Use last breakpoint for index at or above upper limit: 'on'
     Remove protection against out-of-range input in generated code: 'off'
   */
  if (u0 <= bp0[0UL]) {
    iLeft = 0UL;
    frac = 0.0F;
  } else if (u0 < bp0[maxIndex]) {
    /* Binary Search */
    bpIdx = maxIndex >> 1UL;
    iLeft = 0UL;
    iRght = maxIndex;
    while (iRght - iLeft > 1UL) {
      if (u0 < bp0[bpIdx]) {
        iRght = bpIdx;
      } else {
        iLeft = bpIdx;
      }

      bpIdx = (iRght + iLeft) >> 1UL;
    }

    frac = (u0 - bp0[iLeft]) / (bp0[iLeft + 1UL] - bp0[iLeft]);
  } else {
    iLeft = maxIndex;
    frac = 0.0F;
  }

  /* Interpolation 1-D
     Interpolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'on'
     Overflow mode: 'portable wrapping'
   */
  if (iLeft == maxIndex) {
    y = table[iLeft];
  } else {
    y = (table[iLeft + 1UL] - table[iLeft]) * frac + table[iLeft];
  }

  return y;
}

/* Model step function */
void SOH_cycle_step(void)
{
  real32_T rtb_Switch1;
  uint32_T tmp;
  real32_T tmp_0;
  real32_T tmp_1;

  /* Abs: '<S2>/Abs' incorporates:
   *  Inport: '<Root>/BatCurrent'
   */
  if (BatCurrent < 0.0F) {
    tmp_0 = floorf(-BatCurrent);
    if (rtIsNaNF(tmp_0) || rtIsInfF(tmp_0)) {
      tmp_0 = 0.0F;
    } else {
      tmp_0 = fmodf(tmp_0, 4.2949673E+9F);
    }

    tmp = tmp_0 < 0.0F ? (uint32_T)-(int32_T)(uint32_T)-tmp_0 : (uint32_T)tmp_0;
  } else {
    tmp_0 = floorf(BatCurrent);
    if (rtIsNaNF(tmp_0) || rtIsInfF(tmp_0)) {
      tmp_0 = 0.0F;
    } else {
      tmp_0 = fmodf(tmp_0, 4.2949673E+9F);
    }

    tmp = tmp_0 < 0.0F ? (uint32_T)-(int32_T)(uint32_T)-tmp_0 : (uint32_T)tmp_0;
  }

  /* End of Abs: '<S2>/Abs' */

  /* Product: '<S2>/Product' incorporates:
   *  Product: '<S2>/Divide'
   */
  rtb_Switch1 = (real32_T)tmp / 360000.0F;

  /* Outport: '<Root>/DCapacity' */
  SOH_cycle_Y.DCapacity = rtb_Switch1;

  /* Switch: '<S2>/Switch2' incorporates:
   *  Constant: '<S2>/Constant4'
   *  Inport: '<Root>/BatCurrent'
   *  RelationalOperator: '<S2>/Relational Operator'
   */
  if (BatCurrent <= 0.0F) {
    tmp_0 = rtb_Switch1;
  } else {
    tmp_0 = 0.0F;
  }

  /* End of Switch: '<S2>/Switch2' */

  /* Switch: '<S2>/Switch3' incorporates:
   *  Constant: '<S2>/Constant8'
   *  Inport: '<Root>/AccDchaAH_EE'
   *  UnitDelay: '<S2>/Unit Delay4'
   */
  if (SOH_cycle_DW.UnitDelay4_DSTATE != 0L) {
    tmp_1 = 0.0F;
  } else {
    tmp_1 = AccDchaAH_EE;
  }

  /* End of Switch: '<S2>/Switch3' */

  /* Sum: '<S2>/Add2' incorporates:
   *  UnitDelay: '<S2>/Unit Delay2'
   */
  AccDchaAH = (AccDchaAH + tmp_0) + tmp_1;

  /* Switch: '<S1>/Switch1' incorporates:
   *  Constant: '<S1>/Constant9'
   *  Inport: '<Root>/BatCurrent'
   *  RelationalOperator: '<S1>/Relational Operator1'
   */
  if (!(BatCurrent > 0.0F)) {
    rtb_Switch1 = 0.0F;
  }

  /* End of Switch: '<S1>/Switch1' */

  /* Switch: '<S1>/Switch4' incorporates:
   *  Constant: '<S1>/Constant11'
   *  Inport: '<Root>/AccChaAH_EE'
   *  UnitDelay: '<S1>/Unit Delay5'
   */
  if (SOH_cycle_DW.UnitDelay5_DSTATE != 0L) {
    tmp_0 = 0.0F;
  } else {
    tmp_0 = AccChaAH_EE;
  }

  /* End of Switch: '<S1>/Switch4' */

  /* Sum: '<S1>/Add3' incorporates:
   *  UnitDelay: '<S1>/Unit Delay3'
   */
  AccChaAH = (AccChaAH + rtb_Switch1) + tmp_0;

  /* Lookup_n-D: '<S3>/SOH_Life' incorporates:
   *  Gain: '<S3>/Gain'
   *  Gain: '<S3>/Gain1'
   *  MinMax: '<S3>/MinMax'
   */
  BatSoh = look1_iflf_binlcapw(fmaxf(0.13333334F * AccDchaAH, 0.13333334F *
    AccChaAH), SOH_cycle_ConstP.SOH_Life_bp01Data,
    SOH_cycle_ConstP.SOH_Life_tableData, 4UL);

  /* Update for UnitDelay: '<S2>/Unit Delay4' incorporates:
   *  Constant: '<S2>/Constant6'
   */
  SOH_cycle_DW.UnitDelay4_DSTATE = 1L;

  /* Update for UnitDelay: '<S1>/Unit Delay5' incorporates:
   *  Constant: '<S1>/Constant10'
   */
  SOH_cycle_DW.UnitDelay5_DSTATE = 1L;
}

/* Model initialize function */
void SOH_cycle_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* initialize error status */
  rtmSetErrorStatus(SOH_cycle_M, (NULL));

  /* states (dwork) */
  (void) memset((void *)&SOH_cycle_DW, 0,
                sizeof(DW_SOH_cycle_T));

  /* external outputs */
  (void) memset((void *)&SOH_cycle_Y, 0,
                sizeof(ExtY_SOH_cycle_T));
}

/* Model terminate function */
void SOH_cycle_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
